#pragma once

#include <windows.h>
#include "GDInterface.h"

using namespace System;
using namespace System::Windows::Forms;
//using namespace System::Drawing;

namespace PaintForm 
{
	public ref class CPaintWindow: public System::Windows::Forms::NativeWindow
	{
	public:
		CPaintWindow(System::Windows::Forms::Form ^ parentForm, GLsizei iWidth, GLsizei iHeight)
		{
			CreateParams^ cp = gcnew CreateParams;

			// Set the position on the form
			cp->X = 0;
			cp->Y = 25;
			cp->Height = iHeight;
			cp->Width = iWidth;

			// Specify the form as the parent.
			cp->Parent = parentForm->Handle;

			// Create as a child of the specified parent and make OpenGL compliant (no clipping)
			cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

			// Create the actual window
			this->CreateHandle(cp);

			graphic = new GDInterface();
			graphic->Init(1,(HWND)this->Handle.ToPointer());
			graphic->setBackColor(GD_BLACK);

			ReSizeGLScene(iWidth,iHeight);
		}

		System::Void Render(array<System::Drawing::Color> ^ColTable)
		{
			graphic->clear();

			graphic->setColor(GD_GREEN);
			graphic->drawPoint(0,0);
			graphic->drawLine(50,-50,50,50);
			graphic->setColor(GD_RED);
			graphic->setPointRadius(3);
			graphic->setLineWidth(3);
			graphic->drawPoint(10,10);
			graphic->drawLine(60,-50,60,50);
			graphic->setColor(GD_BLUE);
			graphic->setPointRadius(2);
			graphic->setLineWidth(5);
			graphic->drawPoint(20,20);
			graphic->drawLine(70,-50,70,50);

			graphic->setColor(GD_CYAN);
			graphic->drawGradientFilledTriangle(-50,-50,-50,50,-25,0,GD_RED,GD_GREEN,GD_BLUE);
			//graphic->drawFilledTriangle(-50,-50,-50,50,-25,0,GD_BLUE);

			graphic->setLineWidth(2);
			graphic->setColor(GD_YELLOW);
			double coords[10] = {-99, 55, -99, 99, -50, 99, 0, 85, 0, 80};
			graphic->drawFilledPolygon(coords,5,GD_MAGENTA);

			graphic->setColor(GD_YELLOW);
			graphic->drawText(100,650,"Hello,World123!");
			graphic->draw();
		}

	private:
		GDInterface* graphic;

	protected:
		~CPaintWindow(System::Void)
		{
			graphic->Deinit((HWND)this->Handle.ToPointer());
			this->DestroyHandle();
		}

		GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
		{
			if (height==0)										// Prevent A Divide By Zero By
			{
				height=1;										// Making Height Equal One
			}
			
			graphic->setResolution(width,height);
			graphic->setCoordinates(-100,100,-100,100);
		}
	};
}